<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="true" %>

<script>
function fncOrder(obj){
	return fncOrderMember('2',obj);   //회원
}

///////////////////////////////////////////////////////////////////////////////
//  선택된 상품만 (삭제,찜)                                                  //
///////////////////////////////////////////////////////////////////////////////
function chkPrd(mode){
 var checkPrd = new Array();
   $('input:checkbox[name=chkSign]:checked').each(function(i){       
      checkPrd[i] = $(this).get(0).value;
   });
   //* 선택된상품삭제
   if(mode=="delSel"){
     if(checkPrd<=0){
       alert("상품을 선택해주세요");
       return false;
     }
     //for(var i=0;i<checkPrd.length;i++){
       deleteCartAllUser(checkPrd);
     //}
   //* 선택된상품찜
   }else if(mode=="wishList"){
     if(checkPrd<=0){
       alert("상품을 선택해주세요");
       return false;
     }
     
     updateWishList(checkPrd);
   }
}

///////////////////////////////////////////////////////////////////////////////
//  회원 주문페이지로                                                        //
///////////////////////////////////////////////////////////////////////////////
function fncOrderMember(flag,obj){
  var chkSign = $("input[name='chkSign']:checked");
  if( chkSign.length <= 0 ){
    alert("구매하실 상품을 선택하세요.");
    return false;
  }
  
  //선택된것담기
  var prdSap = new Array();
  $('input:checkbox[name=chkSign]:checked').each(function(i){       
     prdSap[i] = $(this).get(0).value;
  });
  //상품없이 주문할때
  if(prdSap.length<=0 && obj!="A"){
    alert("구매하실 상품을 선택하세요.");
    return false;
  }
  
  //장바구니
  var cartListTmp = getCartList();
  var cartList = '';
  if(cartListTmp == '0'){
    cartList = eval('(' + '[]' + ')');
  }else{
    cartList = eval('(' + cartListTmp + ')');
  }
  
  $.cookies.set('orderList','[]');  //초기화       
  var orderList = $.cookies.get('orderList');
  
  //체크선택
  for(var i=0;i<prdSap.length;i++){
      for(var j=0;j<cartList.length;j++){
          if(prdSap[i]==cartList[j].a){
        	   orderList.push( {a: cartList[j].a, b: Number(cartList[j].b), c: Number(cartList[j].c), d: cartList[j].d} );
      	  }
      }
  }
  setCartList('update2',orderList);
  location.href="/vhmall/order.vh";
}
	
/////////////////////////////////////////////////////////////////////////////// 
//  회원 찜 선택추가                                                         //
/////////////////////////////////////////////////////////////////////////////// 
function updateWishList(prdCd) {
  var wishListTmp = getWishList();
  var wishList    = '';
  var isMode      = '';
  if(wishListTmp == '0'){
    wishList  = eval('(' + '[]' + ')');
  }else{
    wishList  = eval('(' + wishListTmp + ')');
  }    
  if(wishListTmp=="[]"){
    isMode= "update";
  }
  for(var i=0;i<prdCd.length;i++){
    for(var j=0;j<wishList.length;j++){       
       if(prdCd[i]==wishList[j].a){
       alert("이미 찜하기 되어 있는 상품입니다.");
       return false;       
      }else{
        isMode= "update";
      }
    }
  }   
  //첫입력
  if(wishListTmp == '0'){
    for(var k=0;k<prdCd.length;k++){
      wishList.push( {a: prdCd[k]} );
    }            
    setCartList('insertWish',wishList);
    alert("찜리스트에 추가하였습니다.");
  }    
  //업데이트
  if(isMode=="update"){
    for(var k=0;k<prdCd.length;k++){
      wishList.push( {a: prdCd[k]} );
    } 
    setCartList('updateWish',wishList);
    alert("찜리스트에 추가하였습니다.");
  }
  location.href="/vhmall/wishlist.vh";
}
</script>

<script>
function count_checkobx() {
	var cnt = 0;
	$("input[name='chkSign']:checkbox").each(function(){ 
    	if($(this).is(":checked")) {
    		cnt++;
    	}
    }); 
    
    $("#cart_cnt").html(cnt);
}

$(document).ready(function() {
	$("#all_checkbox").click(function() {
		var check = $("#all_checkbox").is(":checked"); 
	    $("input[name='chkSign']:checkbox").each(function(){ 
	    	$(this).prop("checked", check);
	    }); 
	    
	    count_checkobx();
	});
	
	$("input[name='chkSign']:checkbox").click(function() {
	    count_checkobx();
	});
});
</script>

<!-- 콘텐츠 영역 -->
    <div id="BodyWrap">
        <section>
            <h4 class="pag_title" title="장바구니">장바구니</h4>
            <div id="ContentWrap4">
            	<div class="mar_b10">
                	<div class="inp_ckb inp_ckb3">
                        <input type="checkbox" class="ckb" name="all_checkbox" id="all_checkbox">
                        <label for="all_checkbox">주문상품 총 <span class="ftc_red" id="cart_cnt">0</span>개</label>
                    </div>
                </div>
                <form name="cartForm" method="post">
                	<input type="hidden" name="cstmno" value="${sessionScope.cstmno}" />
                
                <ul class="showwindow_list3">
                	<c:forEach items="${requestScope.itemList}" var="items" varStatus="status">
	                	<li>
	                    	<div class="inp_ckwrap2">
	                        	<div class="inp_ckb2">
	                            	<input type="checkbox" class="ckb" name="chkSign" id="chkSign${status.index}" value="${items.PRDCD}_${items.SAPCD}">
	                                <label for="all_checkbox">&nbsp;</label>
	                            </div>
	                        </div>
	                    	<a href="${contextPath}/vhmall/item_detail.vh?prdcd=${items.PRDCD}" class="thumb">
	                            <img src="http://www.vhmall.co.kr/inc/imgLoad.jsp?dirName=product/${items.CORPNO}/&fileName=${items.PRDIMG1_ENC}&thumb=Y&quality=100&size=230" />
	                        </a>
	                        <div class="windowinfo_area">
	                            <a href="/vhmall/item_detail.vh?prdcd=${items.PRDCD}" class="title">${items.PRDNM }</a>
	                            <p class="price">
	                                ${items.SELPRC}원
	                            </p>
	                            <p>
	                            	${items.PRDNONM} &nbsp;&nbsp;&nbsp;${items.BUYQTY}개
	                            </p>
	                            <p class="btnposition_box">
	                                <a href="#" class="btn_inlistyle1 gradation_gray1" title="삭제하기" onclick="deleteCartUser('${items.PRDCD}_${items.SAPCD}');">삭제하기</a>
	                                <a href="javascript:alert('준비중입니다.');" class="btn_inlistyle1 gradation_gray1 ftc_darkred" title="1:1문의">1:1문의</a>
	                            </p>
	                        </div>
	                    </li>
                    </c:forEach>
                </ul>
                </form>
                <div class="btnposition_itembuy">
                    <div class="btnwline3">
                        <a href="javascript:chkPrd('wishList');" class="heart" title="찜하기">찜하기</a>
                        <a href="javascript:chkPrd('delSel');" class="delete" title="삭제하기">삭제하기</a>
                        <a href="javascript:fncOrder('S');" title="주문하기">주문하기</a>
                    </div>
                </div>
            </div>
        </section>
        <!--// 콘텐츠 영역 -->